package com.ecric.http.OkHttp3.package3;

import android.text.TextUtils;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * <h3>Description</h3>
 * TODO
 * <h3>Author</h3> luzhenbang
 * <h3>Date</h3> 04/04/2018 16:21
 * <h3>Copyright</h3> Copyright (c)2018 Shenzhen TL  Co., Ltd. Inc. All rights reserved.
 */
public class PostRequestBuilder {

    /** Json类型*/
    public static final MediaType JSON  = MediaType.parse("application/json; charset=utf-8");
    /** 图片*/
    private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

    private String url;
    private Object tag;
    private String json;
    /** 头部*/
    private Map<String, String> headers;
    /** 参数*/
    private Map<String, String> params;
    private List<FileInput> files;


    public PostRequestBuilder(String url, Object tag,
                              String json, Map<String, String> headers,
                              Map<String, String> params,
                              List<FileInput> files) {
        this.url = url;
        this.tag = tag;
        this.json = json;
        this.headers = headers;
        this.params = params;
        this.files = files;
//        buildRequestBody(builder);
//        buildRequestBody();
    }

    public Request buildRequestBody() {
        Request.Builder builder = new Request.Builder();

        RequestBody requestBody = null;
        if (!TextUtils.isEmpty(json)){
            requestBody = RequestBody.create(JSON,json);
        }else  if (params != null){
            FormBody.Builder form = new FormBody.Builder();
            Set<Map.Entry<String,String>> entries = params.entrySet();
            Iterator<Map.Entry<String,String>> iterator  = entries.iterator();
            while (iterator.hasNext()){
                Map.Entry<String,String> entry = iterator.next();
                form.add(entry.getKey(),entry.getValue());
            }
            requestBody = form.build();
        }else if (null != files){
            MultipartBody.Builder  multi = new MultipartBody.Builder();
            for (int i = 0; i < files.size(); i++) {
                multi.addFormDataPart(files.get(i).key,files.get(i).filename,
                        RequestBody.create(MEDIA_TYPE_PNG,files.get(i).file));
            }
            requestBody  = multi.build();
        }else {
            requestBody = new FormBody.Builder().build();
        }

        if (null != headers && headers.size() > 0){
            Set<Map.Entry<String,String>> entries = headers.entrySet();
            Iterator<Map.Entry<String,String>> iterator  = entries.iterator();
            while (iterator.hasNext()){
                Map.Entry<String,String> entry = iterator.next();
                builder.addHeader(entry.getKey(),entry.getValue());
            }
        }

        if (null != tag){
            builder.tag(tag);
        }

       return builder.url(url).post(requestBody).build();
    }

    public RequestCall build(){
        return new RequestCall(buildRequestBody());
    }



}
